## RUN ALL SCRIPTS

# PLEASE RESET THE R SESSION BEFORE RUNNING THIS SCRIPT
# RUNNING ALL SCRIPTS TAKES APPROXIMATELY 3.5 HOURS, DEPENDING ON COMPUTING POWER


con <- file("run_all.log")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")
library(tictoc)
tic()

# create directories to store output
if(!dir.exists('results')){
dir.create("results")
}

if(!dir.exists('figures')){
  dir.create("figures")
}

if(!dir.exists('tables')){
  dir.create("tables")
}



#### Figure 1
rm(list=ls())
gc()
source('Fig1.R', echo = T,  max.deparse.length=10000)



#### Figures 2 and 3, Tables 1 and 2, Figures S1-S4 and Figure S10 Figures S24-S25
rm(list=ls())
gc()
source('primary_analysis_state_loop.R', echo = T,  max.deparse.length=10000)
source('Fig2_Fig3_Tab1_Tab2_FigS1_FigS2_FigS3_FigS4_FigS10_FigS24_FigS25.R', echo = T,  max.deparse.length=10000)


# Table 3, Figure S5-S8
rm(list=ls())
gc()
source('primary_analysis_state_loop_other.R', echo = T,  max.deparse.length=10000)
source('Tab3_FigS5_FigS6_FigS7_FigS8.R', echo = T,  max.deparse.length=10000)


# Figure S1-S8 already created in prior scripts

# Figure S9
rm(list=ls())
gc()
source('FigS9.R', echo = T,  max.deparse.length=10000)


# figure S10 already created in prior script


# Figure S11 and Figure S12
rm(list=ls())
gc()
source('primary_analysis_state_loop_bw.R', echo = T,  max.deparse.length=10000)
source('FigS11_FigS12.R', echo = T,  max.deparse.length=10000)


# Figures S13-S19
rm(list=ls())
gc()
source('primary_analysis_state_loop_specifications.R', echo = T,  max.deparse.length=10000)
source('FigS13_FigS14_FigS15_FigS16_FigS17_FigS18_FigS19.R', echo = T, max.deparse.length=10000)


# Figures S20-S23

rm(list=ls())
gc()
source('primary_analysis_gender_state_loop.R', echo = T, max.deparse.length=10000)
source('FigS20_FigS21_FigS22_FigS23.R', echo = T, max.deparse.length=10000)

# Figures S24 and S25 created in prior script


# Figures S26-S29

rm(list=ls())
gc()
source('primary_analysis_state_loop_nonwhite.R', echo = T, max.deparse.length=10000)
source('FigS26_FigS27_FigS28_FigS29.R', echo = T, max.deparse.length=10000)



toc()

####################3
# Restore output to console
sink() 
sink(type="message")


